package leetcode.秋招.小红书;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @author ：zsy
 * @date ：Created 2021/8/21 11:16
 * @description：\
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        in.nextLine();
        PriorityQueue<Theme> priorityQueue = new PriorityQueue<>(new Comparator<Theme>() {
            @Override
            public int compare(Theme o1, Theme o2) {
                if (o1.start.compareTo(o2.start) != 0) {
                    return o1.start.compareTo(o2.start);
                } else {
                    return o1.end.compareTo(o2.end);
                }
            }
        });
        for (int i = 0; i < N; i++) {
            String times = in.nextLine();
            int index = times.indexOf("-");
            Theme theme = new Theme(times.substring(0, index), times.substring(index + 1));
            priorityQueue.offer(theme);
        }
        String curTime = priorityQueue.peek().start;
        int count = 0;
        int maxTime = 0;
        int pre = 0;
        while (!priorityQueue.isEmpty()) {
            Theme cur = priorityQueue.poll();
            if (count == 3) {
                break;
            } else if (curTime.compareTo(cur.start) > 0) {
                if (cur.getTime() > pre) {
                    maxTime -= pre;
                    maxTime += cur.getTime();
                    curTime = cur.end;
                }

                continue;
            } else {
                count++;
                maxTime += cur.getTime();
                pre = cur.getTime();
                curTime = cur.end;
            }
        }
        System.out.println(count >= 3 ? maxTime : 0);
    }

    static class Theme {
        String start;
        String end;

        public Theme(String start, String end) {
            this.start = start;
            this.end = end;
        }

        public int getTime() {
            int index = end.indexOf(":");
            int index1 = start.indexOf(":");
            int min = (Integer.parseInt(end.substring(0, index)) - Integer.parseInt(start.substring(0, index1))) * 60;
            return min + (Integer.parseInt(end.substring(index + 1)) - Integer.parseInt(start.substring(index1 + 1)));
        }

        @Override
        public String toString() {
            return "Theme{" +
                    "start='" + start + '\'' +
                    ", end='" + end + '\'' +
                    '}';
        }
    }
}
